package musicstore.web.controller;

import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import musicstore.biz.AlbumBiz;
import musicstore.biz.GenreBiz;
import musicstore.web.Security.Authoritize;

@Aspect
@Authoritize(rules="管理员")
@Controller
@RequestMapping("/admin")
public class ManageController {
	@Autowired
	private AlbumBiz albumbiz;
	@Autowired
	private GenreBiz genrebiz;
	@RequestMapping("/manage")
	public String manage(Model model){
		model.addAttribute("categories", genrebiz.getAll());
		return "admin/manage";
	}
	
	@RequestMapping("/product-list")
	public String product(Integer genreId, String title, Integer pageNum, Model model){
		genreId = genreId == null ? 0 : genreId;
		pageNum = pageNum == null ? 1 : pageNum;
		int pageSize = 10;
		int rows = albumbiz.getRows(genreId, title);
		int totalPages = rows % pageSize == 0 ? rows / pageSize : rows / pageSize + 1;
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pagetotal", totalPages);
		model.addAttribute("categories", genrebiz.getAll());
		model.addAttribute("albumlist", albumbiz.fetchAlbumspage(genreId, title, pageNum, pageSize));
		model.addAttribute("categories", genrebiz.getAll());
		return "admin/product-list";
	}
	@RequestMapping("/manage-genre")
	public String genre(Model model){
		model.addAttribute("categories", genrebiz.getAll());
		return "admin/manage-genre";
	}
	@RequestMapping("/order-list")
	public String order(Model model){
		model.addAttribute("categories", genrebiz.getAll());
		return "admin/order-list";
	}
}
